package com.android.awacs.wifi;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class WiFiScanModule {

	private ArrayList<WiFiModule> modules;

	public WiFiScanModule() {
		modules = new ArrayList<WiFiModule>();
	}

	public ArrayList<WiFiModule> scanStart(ArrayList<String> ips) throws InterruptedException {
		for (String ip : ips) {
			Thread.sleep(500);
			String str = "#?";
			try {
				Socket socket = new Socket(ip, 1919);
				BufferedReader plec = new BufferedReader(
						new InputStreamReader(socket.getInputStream()));

				PrintWriter pred = new PrintWriter(
						new BufferedWriter(
								new OutputStreamWriter(socket.getOutputStream())),
								true);

				pred.println(str);
				str = plec.readLine();

				String[] resultat = str.split(" ");
				if (resultat[0].compareTo("OK") == 0) {
					WiFiModule m = new WiFiModule(ip);
					for (int i = 1; i < resultat.length; i++) {
						if (resultat[i].compareTo("LUM") == 0
								|| resultat[i].compareTo("GPS") == 0
								|| resultat[i].compareTo("THM") == 0
								|| resultat[i].compareTo("DOO") == 0) {
							m.addService(resultat[i]);
						}
					}
					this.modules.add(m);
				}
				plec.close();
				pred.close();
				socket.close();
			} catch (Exception e) {
			}
		}
		return this.modules;
	}
}